#!/usr/bin/env python
#
#       Sounds.py
#       
#       Copyright 2009 Robin Hayes <bonzairob@hotmail.co.uk>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import pygame

import Options

from Client import Client
client = Client()

class Sounds:
    def __init__(self):
        #Let's get the player-set stuff.
        self.musvol = Options.VOLUME_MUSIC
        self.sfxvol = Options.VOLUME_SFX
        self.mute = not Options.SOUND
        
        sounds = ("steamcannons.ogg",
                  "nextturn.ogg",
                  "hit.ogg",
                  "crit.ogg")
        tunes = ("Cogs.ogg",
                 "Battle1.ogg")
                
        if self.mute:
            print "Muted."
            #Don't bother loading sounds if the player doesn't want them.
        else:
            
            #TODO: Method for the laoding of sounds before a battle, instead of loading it all at once.
            
            pygame.mixer.init()
            #AAAH WHY HAS IT FROZEN
            print "Loading sounds."
            
            # Initialise mixer channels
            self.channels = []
            for channel in range(pygame.mixer.get_num_channels()):
                self.channels.append(pygame.mixer.Channel(channel))
                self.channels[-1].set_volume(self.sfxvol)
            
            self.sounds = {}
            # For each sound we want to load
            for sound in sounds:
                path = os.path.join("Data", "Sounds", sound)
                # If the file exists
                if os.path.isfile(path):
                    # Try to load it
                    self.sounds[sound] = pygame.mixer.Sound(path)
                else:
                    self.sounds[sound] = None
                    
            self.tunes = {}
            # For each tune we want to load
            for tune in tunes:
                path = os.path.join("Data", "Sounds", tune)
                # If the file exists
                if os.path.isfile(path):
                    # Try to load it
                    self.tunes[tune] = path
                else:
                    self.tunes[tune] = None

            
    def sfx(self, sound):
        # If user wants to play sounds
        if not self.mute:
            try:
                # Try to get the sound effect from dictionary
                effect = self.sounds[sound]
            except KeyError:
                # If sound isn't loaded, return False
                return False
            # Try to get an idle mixer channel
            channel = pygame.mixer.find_channel()
            # If we find one
            if channel:
                # Play the sound effect
                channel.play(effect)
                # Volume is reset to max when each sound is played,
                # so we must set it each time
                channel.set_volume(self.sfxvol)
                # Return True on success
                return True
        return False

    def music(self, tune, loop=0, fade=False):
        # If user wants to play sounds
        if not self.mute:
            if fade:
                pygame.mixer.music.fadeout(1000)
            else:
                pygame.mixer.music.stop()
            try:
                # Try to load the tune onto the music mixer
                pygame.mixer.music.load(self.tunes[tune])
            except KeyError:
                # If tune isn't loaded, return False
                return False
            pygame.mixer.music.play(loop)
            # Volume is reset to max when each tune is played,
            # so we must set it each time
            pygame.mixer.music.set_volume(self.musvol)
            return True
        return False
        
    def set_sfx_volume(self, value):
        if not self.mute:
            #For later, with GUI options. SLIDERZ YEAH
            if value <= 1 and value >= 0:
                self.sfxvol = value
                for channel in self.channels:
                    channel.set_volume(value)
                if self.sfxvol == 0 and self.musvol == 0:
                    self.mute = True
                else:
                    self.mute = False
                
    def set_music_volume(self, value):
        if not self.mute:
            if value <= 1 and value >= 0:
                self.musvol = value
                pygame.mixer.music.set_volume(value)
                if self.sfxvol == 0 and self.musvol == 0:
                    self.mute = True
                else:
                    self.mute = False
